package com.app.utils.util.view.expression.view;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.TextView;

import com.app.utils.util.LinkMovementClickMethod;
import com.app.utils.util.view.expression.ExpressionUtil;
import com.app.utils.util.view.expression.listener.Clickable;
import com.app.utils.util.view.expression.listener.LinkMovementMethodOverride;

import java.util.ArrayList;

/**
 * 可解析表情的TextView
 *
 * @author xuxinghai
 */
public class BiaoQinTextView extends TextView {

    public BiaoQinTextView(Context context) {
        super(context);
    }

    public BiaoQinTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    /**
     * 设置表情字符
     * @param text
     */
    public void setPicText(CharSequence text){

        try {
            SpannableString spannableString = ExpressionUtil.getExpressionString(getContext(), text);
            setText(spannableString);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置多样式的文字(集合长度必须保持一致)
     * @param str
     * @param color
     * @param clickListener
     */
    public void setText(ArrayList<String> str, ArrayList<Integer> color,ArrayList<Object> parms,
                        Clickable.ClickListener clickListener) {
        // 累加数组所有的字符串为一个字符串
        StringBuffer long_str = new StringBuffer();
        for (int i = 0; i < str.size(); i++) {
            long_str.append(str.get(i));
        }
        //解析表情
        SpannableString builder = null;
        try {
            builder = ExpressionUtil.getExpressionString(getContext(), long_str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }

        if(builder == null){
            setText(builder);
            return;
        }

        // 获取不同字符串的颜色
        ArrayList<ForegroundColorSpan> foregroundColorSpans = new ArrayList<ForegroundColorSpan>();
        for (int i = 0; i < color.size(); i++) {
            foregroundColorSpans.add(new ForegroundColorSpan(color.get(i)));
        }

//        // 设置不同字符串的字号
//        ArrayList<AbsoluteSizeSpan> absoluteSizeSpans = new ArrayList<AbsoluteSizeSpan>();
//        for (int i = 0; i < color.size(); i++) {
//            absoluteSizeSpans.add(new AbsoluteSizeSpan(ViewSizeUtils.sp2px(context,
//                    text_size_list.get(i))));
//        }

        // 设置不同字符串的点击事件和文字颜色
        for (int i = 0; i < str.size(); i++) {
            int p = i;
            int star = long_str.toString().indexOf(str.get(i)+"");
            int end = star + (str.get(i)+"").length();
            //设置点击事件
            builder.setSpan(new Clickable(clickListener, p,parms), star, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //设置颜色

            builder.setSpan(foregroundColorSpans.get(i), star, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

//            //设置字体大小
//            builder.setSpan(absoluteSizeSpans.get(i), star, end,
//                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

//        // 设置点击后的颜色为透明，否则会一直出现高亮
        setHighlightColor(Color.TRANSPARENT);
        setClickable(true);
        setText(builder);
        setMovementMethod(LinkMovementMethod.getInstance());
        setOnTouchListener(new LinkMovementMethodOverride());


    }


    /**
     * 设置多样式的文字(集合长度必须保持一致) 没有maxline限制的
     * @param str
     * @param color
     * @param clickListener
     */
    public void setTextNoMax(ArrayList<String> str, ArrayList<Integer> color,ArrayList<Object> parms,
                        Clickable.ClickListener clickListener) {
        // 累加数组所有的字符串为一个字符串
        StringBuffer long_str = new StringBuffer();
        for (int i = 0; i < str.size(); i++) {
            long_str.append(str.get(i));
        }
        //解析表情
        SpannableString builder = null;
        try {
            builder = ExpressionUtil.getExpressionString(getContext(), long_str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }

        if(builder == null){
            setText(builder);
            return;
        }

        // 获取不同字符串的颜色
        ArrayList<ForegroundColorSpan> foregroundColorSpans = new ArrayList<ForegroundColorSpan>();
        for (int i = 0; i < color.size(); i++) {
            foregroundColorSpans.add(new ForegroundColorSpan(color.get(i)));
        }



        // 设置不同字符串的点击事件和文字颜色
        for (int i = 0; i < str.size(); i++) {
            int p = i;
            int star = long_str.toString().indexOf(str.get(i)+"");
            int end = star + (str.get(i)+"").length();
            //设置点击事件
            builder.setSpan(new Clickable(clickListener, p,parms), star, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //设置颜色

            builder.setSpan(foregroundColorSpans.get(i), star, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        }

//        // 设置点击后的颜色为透明，否则会一直出现高亮
        setHighlightColor(Color.TRANSPARENT);
        setClickable(true);
        setText(builder);
        setMovementMethod(LinkMovementClickMethod.getInstance());


    }





}
